/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.core.commands;

import org.eclipse.gef.commands.Command;

import com.cea.papyrus.core.directedit.PropertyAccessor;


/**
 * A Command called when a property is directly edited in the model.
 * The Command is created with an object encapsulating the property getter and setter.
 * This allows to use the same command for different properties.
 */
public class LabelDirectEditCommand extends Command {

	/**
	 * 
	 */
	private String newValue, oldValue;
	
	/**
	 * 
	 */
	private PropertyAccessor property;
	
	/**
	 * Create the command.
	 * 
	 * @param value The value to set to the property.
	 * @param property The property onto which the command should be executed.
	 */
	public LabelDirectEditCommand(PropertyAccessor property, String value) {
		this.property = property;
		if (value != null) {
			newValue = value;
		} else {
			newValue = "";
		}
		setLabel("Edit "+property.getPropertyName());
	}
	
	/**
	 * 
	 */
	@Override
	public void execute() {
		oldValue = property.getValue();
		property.setValue(newValue);
	}
	
	/**
	 * 
	 */
	@Override
	public void undo() {
		property.setValue(oldValue);
	}

	/**
	 * 
	 */
	@Override
	public void redo() {
		property.setValue(newValue);
	}

}
